package com.security.manage.user.config;

import com.security.manage.user.model.Dept;
import com.security.manage.user.model.User;
import com.security.manage.user.repository.DeptRepository;
import com.security.manage.user.repository.UserRepository;
import org.apache.commons.lang.StringUtils;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class InitOperation implements CommandLineRunner {

    @Resource
    private UserRepository userRepository;

    @Resource
    private DeptRepository deptRepository;

    @Override
    public void run(String... args) throws Exception {
        List<Dept> all = deptRepository.findAll();
        List<Long> collect = all.stream().map(Dept::getId).collect(Collectors.toList());
        String deptIds = StringUtils.join(collect, ",");

        User admin = userRepository.findByAccount("admin");
        admin.setManageDept(deptIds);
        userRepository.save(admin);

    }
}
